﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.BLL;
using Register.Command;
using Register.DAL;
using Register.Model;
using System.Web.Script.Serialization;
namespace RegisterWeb.Manager.DoctorPersonalCenter.ashx
{
    /// <summary>
    /// DoctorHandler 的摘要说明
    /// </summary>
    public class DoctorHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request["action"];
            List<DepartmentsInfo> list;
            List<Parameter> list1;
            
            JavaScriptSerializer jss = new JavaScriptSerializer();
            if (action.Equals("departments")) {
                list = DoctorInfoBLL.GetDepartmentsList();
                context.Response.Write(jss.Serialize(list));
            }
            else if (action.Equals("gender")) {
                context.Response.Write("[{\"Gender_ID\":\"男\",\"Gender_Name\":\"男\"},{\"Gender_ID\":\"女\",\"Gender_Name\":\"女\"}]");
            }
            else if (action.Equals("position"))
            {
                list1 = ParamterHelper.GetParametersByType("Doctor_Position");
                context.Response.Write(jss.Serialize(list1));
            }
            else if (action.Equals("education"))
            {
                list1 = ParamterHelper.GetParametersByType("Doctor_Education");
                context.Response.Write(jss.Serialize(list1));
            }
            else if (action.Equals("add")) {
                 DoctorInfo doctor= new DoctorInfo();

                doctor.Doctor_ID = DataIDHelper.GetDataID("Doctor_ID");
                doctor.Departments_ID = context.Request["Departments_ID"];
                doctor.Doctor_No = doctor.Doctor_ID; //临时将ID值赋给编号
                doctor.Doctor_Name = context.Request["Doctor_Name"];
                doctor.Doctor_Gender= context.Request["Doctor_Gender"];
                doctor.Doctor_DateOfBirth = Convert.ToDateTime(context.Request["Doctor_DateOfBirth"]);
                doctor.Doctor_Position = context.Request["Doctor_Position"];
                doctor.Doctor_Education= context.Request["Doctor_Education"];
                doctor.Doctor_Specialty= context.Request["Doctor_Specialty"];
                doctor.Doctor_Resume= context.Request["Doctor_Resume"];
                doctor.Doctor_Phone = context.Request["Doctor_Phone"];
                doctor.Doctor_Email = context.Request["Doctor_Email"];
                doctor.Doctor_Picture= context.Request["Doctor_Picture"];
                doctor.Doctor_AddTime = DateTime.Now;
                doctor.Doctor_Recoder = "DoubleZ";  //临时赋值
                doctor.Doctor_State = "1";
                context.Response.Write(jss.Serialize(new message1(DoctorInfoBLL.AddDoctorInfo(doctor), "医生添加成功！")));
            
            }
            else if (action.Equals("edit"))
            {
                DoctorInfo doctor = new DoctorInfo();
                doctor.Doctor_ID = context.Request["id"];
                doctor.Departments_ID = context.Request["Departments_ID"];

                //doctor.Doctor_No = doctor.Doctor_ID; //临时将ID值赋给编号
                doctor.Doctor_Name = context.Request["Doctor_Name"];
                doctor.Doctor_Gender = context.Request["Doctor_Gender"];
                doctor.Doctor_DateOfBirth =Convert.ToDateTime(context.Request["Doctor_DateOfBirth"]);
                doctor.Doctor_Position = context.Request["Doctor_Position"];
                doctor.Doctor_Education = context.Request["Doctor_Education"];
                doctor.Doctor_Specialty = context.Request["Doctor_Specialty"];
                doctor.Doctor_Resume = context.Request["Doctor_Resume"];
                doctor.Doctor_Phone = context.Request["Doctor_Phone"];
                doctor.Doctor_Email = context.Request["Doctor_Email"];
                doctor.Doctor_Picture = context.Request["Doctor_Picture"];
                doctor.Doctor_LastAmendTime= DateTime.Now;
                doctor.Doctor_Recoder = "DoubleZ";  //临时赋值

                context.Response.Write(jss.Serialize(new message(DoctorInfoBLL.UpdateDoctorInfo(doctor), "编辑医生成功！")));

            }
            else if (action.Equals("delete"))
            {
                String ID = context.Request["id"];
                if (DoctorInfoBLL.DeleteDoctorInfo(ID))
                    context.Response.Write("ok");
                else
                    context.Response.Write("no");
            }
            #region 检查医生用户是否已经存在
            else if (action.Equals("checkUser"))
            {
               string doctorId=context.Request["id"];
                if (DoctorInfoBLL.Exists(doctorId))
                {
                    context.Response.Write("ok");
                }
                else
                {
                    context.Response.Write("no");
                }
            }


            #endregion
            else if (action.Equals("get")) {
                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());
                List<DoctorInfo> list2= new List<DoctorInfo>();
                list2 = DoctorInfoBLL.GetPagedDoctorInfo(row, page, " 1=1 ");

                List<DoctorInfoJSON> json = new List<DoctorInfoJSON>();
                foreach (DoctorInfo doc in list2)
                { //对于每一个医院对象，根据获得的城市ID获得城市名，并绑定
                    json.Add(new DoctorInfoJSON(DoctorInfoBLL.GetDepartmentsNameById(doc.Departments_ID),doc.Departments_ID,Convert.ToString(doc.Doctor_AddTime),Convert.ToString(doc.Doctor_DateOfBirth).Substring(0,9),HospitalInfoBLL.GetParamNameById(doc.Doctor_Education),doc.Doctor_Education,doc.Doctor_Email,doc.Doctor_Gender,doc.Doctor_ID,doc.Doctor_Name,doc.Doctor_No,doc.Doctor_Phone,doc.Doctor_Picture,HospitalInfoBLL.GetParamNameById(doc.Doctor_Position),doc.Doctor_Position,doc.Doctor_Recoder,doc.Doctor_Resume,doc.Doctor_Specialty,doc.Doctor_State));
                }

                 DoctorInfoListJSON DoctorListJSON = new DoctorInfoListJSON();
                 DoctorListJSON.total = HospitalInfoBLL.GetHospitalInfoCount(null);
                 DoctorListJSON.rows = json;

                JavaScriptSerializer jss1 = new JavaScriptSerializer();
                context.Response.Write(jss1.Serialize(DoctorListJSON));
            }
        }
       
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
class message1
{
    public message1(bool success, string msg)
    {
        this.msg = msg;
        this.success = success;
    }
    public bool success { get; set; }
    public string msg { get; set; }
}
class DoctorInfoListJSON
{

    public int total { get; set; }
    public List<DoctorInfoJSON> rows { get; set; }

}
class DoctorInfoJSON
{
    public string Departments_ID
    {
        set;
        get;
    }
    public string Departments_IDValue
    {
        set;
        get;
    }
    public string Doctor_AddTime
    {
        get;
        set;
    }
    public string Doctor_DateOfBirth
    {
        get;
        set;
    }

    public string Doctor_Education
    {
        get;
        set;
    }
    public string Doctor_EducationValue
    {
        get;
        set;
    }
    public string Doctor_Email
    {
        get;
        set;
    }

    public string Doctor_Gender
    {
        get;
        set;
    }

    public string Doctor_ID
    {
        get;
        set;
    }

    public string Doctor_Name
    {
        get;
        set;
    }

    public string Doctor_No
    {
        get;
        set;
    }

    public string Doctor_Phone
    {
        get;
        set;
    }

    public string Doctor_Picture
    {
        get;
        set;
    }

    public string Doctor_Position
    {
        get;
        set;
    }
    public string Doctor_PositionValue
    {
        get;
        set;
    }

    public string Doctor_Recoder
    {
        get;
        set;
    }

    public string Doctor_Resume
    {
        get;
        set;
    }

    public string Doctor_Specialty
    {
        get;
        set;
    }

    public string Doctor_State
    {
        get;
        set;
    }


    public DoctorInfoJSON(string Departments_ID,string Departments_IDValue,string Doctor_AddTime,string Doctor_DateOfBirth,string Doctor_Education,string Doctor_EducationValue,string Doctor_Email,string Doctor_Gender,string Doctor_ID,string Doctor_Name,string Doctor_No,string Doctor_Phone,string Doctor_Picture,string Doctor_Position,string Doctor_PositionValue,string Doctor_Recoder,string Doctor_Resume,string Doctor_Specialty,string Doctor_State)
    {
        this.Departments_ID = Departments_ID;
        this.Departments_IDValue = Departments_IDValue;
        this.Doctor_AddTime = Doctor_AddTime;
        this.Doctor_DateOfBirth = Doctor_DateOfBirth;
        this.Doctor_Education = Doctor_Education;
        this.Doctor_EducationValue = Doctor_EducationValue;
        this.Doctor_Email = Doctor_Email;
        this.Doctor_Gender=Doctor_Gender;
        this.Doctor_ID=Doctor_ID;
        this.Doctor_Name=Doctor_Name;
        this.Doctor_No=Doctor_No;
        this.Doctor_Phone=Doctor_Phone;
        this.Doctor_Picture=Doctor_Picture;
        this.Doctor_Position=Doctor_Position;
        this.Doctor_PositionValue = Doctor_PositionValue;
        this.Doctor_Recoder = Doctor_Recoder ;
        this.Doctor_Resume=Doctor_Resume;
        this.Doctor_Specialty=Doctor_Specialty;
        this.Doctor_State=Doctor_State;
      
    }

}
